﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITM495.OOExamples.Core.BeeHive
{
    public sealed class Queen : Bee
    {
        private int shiftNumber = 0;

        public Queen(List<IWorker>workerBees, double weight): base(weight)
        {
            Workers = workerBees;
        }

        public List<IWorker> Workers{ get; set;}
        
        public int Shift { get; set; }

        public override int ShiftsLeft
        {
            get
            {
                return (from w in Workers select w.ShiftsLeft).Max();
            }
        }

        public bool AssignWork(string job, int numberOfShifts)
        {
            for (int i = 0; i < Workers.Count(); i++)
            {
                if (Workers[i].DoThisJob(job, numberOfShifts))
                    return true;
            }
            return false;

        }

        public string WorkTheNextShift()
        {
            double totalConsumption = 0;

            foreach(IWorker worker in Workers)
            {
                if (worker is Bee)
                {
                    Bee bee = (Bee)worker;
                    totalConsumption += bee.GetHoneyConsumption();
                }
            }

            totalConsumption += GetHoneyConsumption();


            shiftNumber++;
            string report = "Report for shift #" + shiftNumber + "\r\n";
            for (int i = 0; i < Workers.Count(); i++)
            {
                if (Workers[i].WorkOneShift())
                    report += "Worker  " + Workers[i].Name + " finished the job\r\n";
                if (String.IsNullOrEmpty(Workers[i].Job))
                    report += "Worker " + Workers[i].Name + " is not working\r\n";
                else
                    if (Workers[i].ShiftsLeft > 0)
                        report += "Worker " + Workers[i].Name + " is doing ‘" + Workers[i].Job
                                  + "’ for " + Workers[i].ShiftsLeft + " more shifts\r\n";
                    else
                        report += "Worker " + Workers[i].Name + " will be done with ‘"
                                  + Workers[i].Job + "’ after this shift\r\n";
            }

            report += "Total Honey consumption: " + totalConsumption + " units.";

            return report;
        }
        
        public override double GetHoneyConsumption()
        {
            double consumption = 0;
            double largestWorkerConsumption = 0;
            int workersDoingJobs = 0;

            //Free Linq goodness!  select every object that is a bee in Workers and returns the list as an array
            var beeWorkers = Workers.Where(x => x is Bee)
                .Select(x => x as Bee)
                .ToArray();

            for(int i = 0; i< beeWorkers.Count(); i++)
            {
                if (beeWorkers[i].GetHoneyConsumption() > largestWorkerConsumption)
                {
                    largestWorkerConsumption = beeWorkers[i].GetHoneyConsumption();
                }
                if (beeWorkers[i].ShiftsLeft > 0)
                {
                    workersDoingJobs++;
                }
            }

            consumption += largestWorkerConsumption;

            if(workersDoingJobs >= 3)
            {
                consumption += 30;
            }
            else
            {
                consumption += 20;
            }

            return consumption;
        }
        
    }
}